<?php
class M_school_calendar extends CI_Model
{ 
	function generate_calendar($year, $month)
	{
		$prefs = array(
				'show_next_prev' => TRUE,
				'next_prev_url' => base_url() . 'admin/school_calendar'
			);
		$prefs['template'] = '
			{table_open}<table border="2" cellpadding="0" cellspacing="2" class="calendar">{/table_open}
		   {heading_row_start}<tr>{/heading_row_start}

		   {heading_previous_cell}<th class="cal_head" id="left_arrow"><a href="{previous_url}" class="cal_link"><<<</a></th>{/heading_previous_cell}
		   {heading_title_cell}<th class="cal_head" colspan="{colspan}">{heading}</th>{/heading_title_cell}
		   {heading_next_cell}<th class="cal_head" id="right_arrow"><a href="{next_url}" class="cal_link">>>></a></th>{/heading_next_cell}

		   {heading_row_end}</tr>{/heading_row_end}

		   {week_row_start}<tr class="weekday_head">{/week_row_start}
		   {week_day_cell}<td class="weekdays">{week_day}</td>{/week_day_cell}
		   {week_row_end}</tr>{/week_row_end}

		   {cal_row_start}<tr class="days">{/cal_row_start}
		   {cal_cell_start}<td class="this_day">{/cal_cell_start}

		   {cal_cell_content}<div class="day_num">{day}</div><div class="day_content">{content}</div>{/cal_cell_content}
		   {cal_cell_content_today}<div class="day_num" id="today"><strong>{day}</strong></div><div class="day_content">{content}</div>{/cal_cell_content_today}

		   {cal_cell_no_content}<div class="day_num">{day}</div>{/cal_cell_no_content}
		   {cal_cell_no_content_today}<div class="day_num" id="today"><strong>{day}</strong>{/cal_cell_no_content_today}

		   {cal_cell_blank}&nbsp;{/cal_cell_blank}

		   {cal_cell_end}</td>{/cal_cell_end}
		   {cal_row_end}</tr>{/cal_row_end}

		   {table_close}</table>{/table_close}';
		$events = $this->get_events($year, $month);
		$this->load->library('calendar', $prefs);
		return $this->calendar->generate($year, $month,$events);
	}
	
	function get_events($year, $month)
	{
		$events = array();
		
		if(($year == 0) || ($year == '')){
			$year = date('Y', now());
		}
		
		if(($month == 0) || ($month == '')){
			$month = date('m', now());
		}
		
		$query = $this->db->query("SELECT event_desc, event_date 
									FROM school_calendar
									WHERE event_date 
									LIKE '$year-$month%'");
		foreach($query->result() as $row)
		{
			$day = (int)substr($row->event_date, 8, 2);
			$events[(int)$day] = $row->event_desc;
		}
		return $events;
	}
	
	function add_events($date, $event)
	{	
		$query = $this->db->query("SELECT * 
									FROM school_calendar
									WHERE event_date = '$date'");
		if($query->num_rows() > 0)
		{
			echo("Error");
		}else
		{
			$sql = "INSERT INTO school_calendar(event_desc, event_date)
					VALUES (" . $this->db->escape($event) . ", " . $this->db->escape($date) . ")";		
			
			$this->db->query($sql);
		}
	}
	
	function delete_events($date)
	{
		$query = $this->db->query("DELETE FROM school_calendar WHERE event_date = '$date'");
	}
}
?>